package com.solution.admin.point.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.point.entity.PointEntity;
import com.solution.admin.point.entity.PointLogEntity;
import com.solution.admin.point.service.PointLogService;
import com.solution.admin.point.service.PointService;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;

/**
 * 积分信息web控制层
 * 
 * @author xu_cc
 * @date 2017-10-20 14:19:17
 */
@RequestMapping("/point")
@Controller
public class PointController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PointController.class);

    @Autowired
    private PointService pointService;
    @Autowired
    PointLogService pointLogService;

    /**
     * 跳转积分信息查询页面
     * 
     * @param Point
     *            接收界面查询条件参数
     * @param request
     *            请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
     * @date 2017-10-20 14:19:17
     */
    @RequestMapping("/toListPointPage")
    public String toListPointPage(PointEntity point, HttpServletRequest request) {
        return "point/point_list";
    }

    /**
     * 
     * @Title: toPointLogPage
     * @Description:(描述)
     * @param request
     * @param model
     * @param customerId
     * @return String
     * @author xu_cc
     * @throws Exception
     *             exception
     * @Date 创建时间：2017年10月23日 下午4:20:32
     */
    @RequestMapping("/toPointLogPage")
    public String toPointLogPage(HttpServletRequest request, Model model, Long customerId) {
        try {
            PointEntity point = pointService.getPointByCustomerId(customerId);
            model.addAttribute("point", point);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "point/pointLog_list";
    }

    /**
     * 分页查询积分日志信息列表
     * 
     * @param point
     *            接收界面查询条件参数
     * @param request
     *            请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
     * @date 2017-10-20 14:19:17
     */
    @RequestMapping("/listPointLogPage")
    @ResponseBody
    public JSONGrid listPointLogPage(PointLogEntity pointLog, HttpServletRequest request) {
        int pageNum = Integer.parseInt(request.getParameter("page").toString());
        int numPerPage = Integer.parseInt(request.getParameter("rows").toString());
        PageParam pageParam = super.getPageParam(request);
        pageParam.setPageNum(pageNum);
        pageParam.setNumPerPage(numPerPage);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("customerId", pointLog.getCustomerId());
        // 客户公司id 
        String companyId = request.getParameter("companyId");
        paramMap.put("companyId", companyId);
        
        try {
            PageBean pageBean = pointLogService.listPointLogPage(pageParam, paramMap);
            return new JSONGrid(pageBean.getRecordList(), pageBean.getTotalCount());
        } catch (Exception e) {
            handleException("分页查询积分日志信息失败", logger, e);
        }
        return new JSONGrid();
    }

    /**
     * 分页查询积分信息列表
     * 
     * @param point
     *            接收界面查询条件参数
     * @param request
     *            请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
     * @date 2017-10-20 14:19:17
     */
    @RequestMapping("/listPointPage")
    @ResponseBody
    public JSONGrid listPointPage(PointEntity point, HttpServletRequest request, Integer beginPoint, Integer endPoint) {
        int pageNum = Integer.parseInt(request.getParameter("page").toString());
        int numPerPage = Integer.parseInt(request.getParameter("rows").toString());
        PageParam pageParam = super.getPageParam(request);
        pageParam.setPageNum(pageNum);
        pageParam.setNumPerPage(numPerPage);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("customerId", point.getCustomerId());
        paramMap.put("mobile", point.getMobile());
        paramMap.put("customerName", point.getCustomerName());
        paramMap.put("companyName", point.getCompanyName());
        paramMap.put("customerLevel", point.getCustomerLevel());

        paramMap.put("beginPoint", beginPoint);
        paramMap.put("endPoint", endPoint);

        try {
            PageBean pageBean = pointService.listPointPage(pageParam, paramMap);
            return new JSONGrid(pageBean.getRecordList(), pageBean.getTotalCount());
        } catch (Exception e) {
            handleException("分页查询积分信息失败", logger, e);
        }
        return new JSONGrid();
    }

    /**
     * 编辑积分信息
     * 
     * @param request
     *            请求对象
     * @param pointEntity
     *            积分信息实体
     * @return
     * @author xu_cc
     * @date 2017-10-20 14:19:17
     */
    @RequestMapping("/doOperatePoint")
    @ResponseBody
    public BaseReturn savePoint(HttpServletRequest request, PointEntity pointEntity, PointLogEntity pointLog) {
        BaseReturn result = new BaseReturn();
        try {
            pointService.doOperatePointCommon(pointLog.getCustomerId(), pointLog.getOperateFlag(), pointLog.getOperateType(), pointLog.getOperatePoint(),
                    pointLog.getAdjustReason(), null, null, getUserName(), false);
            result.setCode(SysCode.SUCCESS);
            result.setMsg("积分保存成功！");
        } catch (Exception e) {
            result.setCode(SysCode.ERROR);
            result.setMsg("积分保存异常！");
        }
        return result;
    }

}
